<?php
include ('autoloading_classes.include.php');
if ( in_array( strtolower( ini_get( 'magic_quotes_gpc' ) ), array( '1', 'on' ) ) )
{
    $_POST = array_map( 'stripslashes', $_POST );
    $_GET = array_map( 'stripslashes', $_GET );
    $_COOKIE = array_map( 'stripslashes', $_COOKIE );
}
/**
 * Description of NewsManager
 *
 * \author Gianmarco Nalin
 * \date 24/01/2013
 */
class NewsManager extends Manager {
    //! Query per recuperare una news dato il suo id
    const GET_NEWS_BY_ID = "SELECT * FROM news WHERE id = :id;";
    
    //! Query per recuperare tutte le news
    const GET_ALL_NEWS = "SELECT * FROM news WHERE (publication_timestamp <= CURRENT_TIMESTAMP) ORDER BY priority ASC, publication_timestamp DESC;";
    
    //! Query per recuperare tutte le news, anche quelle con timestamp di pubblicazione maggiore dell'attuale
    const GET_ALL_NEWS_ADMIN = "SELECT * FROM news ORDER BY priority ASC, publication_timestamp DESC;";
    
    //! Query per recuperare le ultime news inserite, ordinate per priorità e publication_timestamp.
    const GET_LAST_NEWS = "SELECT * FROM news WHERE (publication_timestamp <= CURRENT_TIMESTAMP) ORDER BY priority ASC, publication_timestamp DESC LIMIT 7;";
    
    //! Query per salvare una nuova news.
    const SAVE_NEWS = "INSERT INTO news (title, content, link, publication_timestamp, image, priority, creator) VALUES (:title, :content, :link, :publication_timestamp, :image, :priority, :creator);";
    
    //! Query per salvare gli aggiornamenti di una news.
    const UPDATE_NEWS = "UPDATE news SET title = :title, content = :content, link = :link, image =:image, publication_timestamp = :publication_timestamp, priority = :priority WHERE id = :id;";
    
    //! Query per eliminare una news.
    const DELETE_NEWS = "DELETE FROM news WHERE id = :id;";
    
    //! Metodo per recuperare le ultime news.
    /*
     * \return Array di News.
     */
    public function get_last_news() {
        $stmt = $this->connection->prepare(self::GET_LAST_NEWS);
        $stmt->execute();
        $news = array();
        $temp = null;
        while ($n = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $temp = new News();
            foreach ($n as $key => $value) {
                if (($key == 'creation_timestamp') || ($key == 'publication_timestamp')) {
                    $temp->{$key} = new DateTime($value);
                } else {
                    $temp->{$key} = $value;
                }
            }
            array_push($news, $temp);
        }
        return $news;
    }
    
    public function get_all_news($admin = false) {
        if ($admin) {
            $stmt = $this->connection->prepare(self::GET_ALL_NEWS_ADMIN);
        } else {
            $stmt = $this->connection->prepare(self::GET_ALL_NEWS);
        }
        $stmt->execute();
        $news = array();
        $temp = null;
        while ($n = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $temp = new News();
            foreach ($n as $key => $value) {
                if (($key == 'creation_timestamp') || ($key == 'publication_timestamp')) {
                    $temp->{$key} = new DateTime($value);
                } else {
                    $temp->{$key} = $value;
                }
            }
            array_push($news, $temp);
        }
        return $news;
    }
    
    //! Metodo per recuperare una news dato il suo id.
    /*
     * \return Oggetto di tipo News.
     */
    public function get_news_by_id($id) {
        $stmt = $this->connection->prepare(self::GET_NEWS_BY_ID);
        $stmt->execute(array('id' => $id));
        $news = null;
        if ($n = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $news = new News();
            foreach ($n as $key => $value) {
                if (($key == 'creation_timestamp') || ($key == 'publication_timestamp')) {
                    $news->{$key} = new DateTime($value);
                } else {
                    $news->{$key} = $value;
                }
            }
        }
        return $news;
    }
    
    //! Metodo per salvare una nuova news.
    /*
     * \param n Nuova news.
     * \return TRUE se il salvataggio è andato a buon fine, FALSE altrimenti.
     */
    public function save_news($news) {
        $stmt = $this->connection->prepare(self::SAVE_NEWS);
        $stmt->execute(array(
            'title' => $news->title,
            'content' => $news->content,
            'link' => $news->link,
            'publication_timestamp' => ($news->publication_timestamp == null) ? date('Y-m-d H:i:s') : $news->publication_timestamp->format('Y-m-d H:i:s'),
            'image' => $news->image,
            'priority' => $news->priority,
            'creator' => $news->creator
        ));
        
        return $stmt->rowCount() > 0;
    }
    
    //! Metodo per salvare gli aggiornamenti di una nuova news.
    /*
     * \param n Oggetto News che contiene gli aggiornamenti da apportare.
     * \return TRUE se il salvataggio è andato a buon fine, FALSE altrimenti.
     */
    public function update_news($news) {
        $stmt = $this->connection->prepare(self::UPDATE_NEWS);
        $stmt->execute(array(
            'id' => $news->id,
            'title' => $news->title,
            'content' => $news->content,
            'link' => $news->link,
            'publication_timestamp' => ($news->publication_timestamp == null) ? date('Y-m-d H:i:s') : $news->publication_timestamp->format('Y-m-d H:i:s'),
            'image' => $news->image,
            'priority' => $news->priority
        ));
        
        return $stmt->rowCount() > 0;
    }
    
    //! Metodo per eliminare una news.
    /*
     * \param id Identificatore della news da eliminare.
     * \return TRUE se il salvataggio è andato a buon fine, FALSE altrimenti.
     */
    public function delete_news($id) {
        $stmt = $this->connection->prepare(self::DELETE_NEWS);
        $stmt->execute(array('id' => $id));
        
        return $stmt->rowCount() > 0;
    }
}
?>
